package com.taotao.content.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.taotao.common.constant.SystemConstant;
import com.taotao.content.service.ContentCategoryService;
import com.taotao.dao.ContentCategoryDao;
import com.taotao.entity.ContentCategory;
import com.taotao.entity.ContentCategoryExample;
import com.taotao.entity.ContentCategoryExample.Criteria;

/**  
 * @Title：ContentCategoryServiceImpl.java   
 * @Description：
 * @Author：张朝阳
 * @Date：2019年7月6日 下午2:53:57   
 */
@Service
public class ContentCategoryServiceImpl implements ContentCategoryService{

	@Autowired
	public ContentCategoryDao contentCategoryDao;
	
	@Override
	public List<ContentCategory> getContentCategoryList(long parentId) {
		ContentCategoryExample example = new ContentCategoryExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdEqualTo(parentId);
		List<ContentCategory> contentCategoryList = contentCategoryDao.selectByExample(example);
		return contentCategoryList;
	}

	@Override
	public ContentCategory addContentCategory(Long parentId, String name) {
		ContentCategory contentCategory = new ContentCategory();
		contentCategory.setParentId(parentId);
		contentCategory.setName(name);
		contentCategory.setStatus(SystemConstant.ValidStatus.VALID.getType());
		contentCategory.setSortOrder(1);
		contentCategory.setIsParent(SystemConstant.ParentFlag.NOTPARENT.getCode());
		contentCategory.setCreated(new Date());
		contentCategory.setUpdated(new Date());
		contentCategoryDao.insert(contentCategory);
		
		//判断父节点状态
		ContentCategory parent = contentCategoryDao.selectByPrimaryKey(parentId);
		
		Integer isParent = parent.getIsParent();
		//如果不是父节点
		if (isParent.equals(SystemConstant.ParentFlag.NOTPARENT.getCode())) {
			isParent = SystemConstant.ParentFlag.PARENT.getCode();
			parent.setIsParent(isParent);
			contentCategoryDao.updateByPrimaryKey(parent);
		}
		return contentCategory;
		
	}

}
